{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6ece1c21",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60d12b7a",
   "metadata": {},
   "source": [
    "# 排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "97ea30f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(np.array([1,5,2,4,3]),)# 数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "cdd97441",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 3, 8, 9, 0],\n",
       "       [0, 8, 5, 9, 0],\n",
       "       [4, 4, 8, 2, 1],\n",
       "       [8, 9, 2, 2, 4],\n",
       "       [2, 1, 7, 4, 9]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1=np.random.randint(0,10,(5,5))\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5a94dd88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 3, 3, 8, 9],\n",
       "       [0, 0, 5, 8, 9],\n",
       "       [1, 2, 4, 4, 8],\n",
       "       [2, 2, 4, 8, 9],\n",
       "       [1, 2, 4, 7, 9]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(arr1,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1787d395",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 2, 0],\n",
       "       [2, 3, 5, 2, 0],\n",
       "       [3, 4, 7, 4, 1],\n",
       "       [4, 8, 8, 9, 4],\n",
       "       [8, 9, 8, 9, 9]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(arr1,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a0e5a1a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr=np.array([1,5,2,4,3])\n",
    "index=np.argsort(arr) # 重新排序值的下标\n",
    "arr[index]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9685b2bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmin(np.array([5,2,1,4,3]))  #返回最小值的索引  dian\n",
    "np.argmax(np.array([1,5,2,4,3]))  #返回最大值的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bc417ceb",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 7, 8, 9])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "073cfd91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 3, 8, 9, 0],\n",
       "       [0, 8, 5, 9, 0],\n",
       "       [4, 4, 8, 2, 1],\n",
       "       [8, 9, 2, 2, 4],\n",
       "       [2, 1, 7, 4, 9]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a8d67b2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 3, 8, 9, 0],\n",
       "       [3, 3, 8, 9, 0],\n",
       "       [3, 3, 8, 9, 0],\n",
       "       [3, 3, 8, 9, 0],\n",
       "       [0, 8, 5, 9, 0],\n",
       "       [0, 8, 5, 9, 0],\n",
       "       [0, 8, 5, 9, 0],\n",
       "       [0, 8, 5, 9, 0],\n",
       "       [4, 4, 8, 2, 1],\n",
       "       [4, 4, 8, 2, 1],\n",
       "       [4, 4, 8, 2, 1],\n",
       "       [4, 4, 8, 2, 1],\n",
       "       [8, 9, 2, 2, 4],\n",
       "       [8, 9, 2, 2, 4],\n",
       "       [8, 9, 2, 2, 4],\n",
       "       [8, 9, 2, 2, 4],\n",
       "       [2, 1, 7, 4, 9],\n",
       "       [2, 1, 7, 4, 9],\n",
       "       [2, 1, 7, 4, 9],\n",
       "       [2, 1, 7, 4, 9]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(arr1,4,axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58c90254",
   "metadata": {},
   "source": [
    "# 统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7a18f484",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17, 25, 30, 26, 14])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.sum(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8f03ddf4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  3,  8,  9,  0],\n",
       "       [ 3, 11, 13, 18,  0],\n",
       "       [ 7, 15, 21, 20,  1],\n",
       "       [15, 24, 23, 22,  5],\n",
       "       [17, 25, 30, 26, 14]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.cumsum(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5780c65",
   "metadata": {},
   "source": [
    "# 改变数组形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5003f6a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 8, 9, 2, 7, 6],\n",
       "       [6, 7, 5, 4, 9, 7],\n",
       "       [8, 6, 7, 7, 7, 9],\n",
       "       [2, 4, 5, 0, 1, 9]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2=np.random.randint(0,10,(4,6))\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "87c31857",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 8, 9, 2, 7, 6, 6, 7],\n",
       "       [5, 4, 9, 7, 8, 6, 7, 7],\n",
       "       [7, 9, 2, 4, 5, 0, 1, 9]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.reshape(3,8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a4be39db",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'expand_dims' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[15], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mexpand_dims\u001b[49m()  \u001b[38;5;66;03m# 深度学习  \u001b[39;00m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'expand_dims' is not defined"
     ]
    }
   ],
   "source": [
    "expand_dims()  # 深度学习  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0757c1f",
   "metadata": {},
   "source": [
    "## 数组合并与切分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fe64e4a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 8, 9, 2, 7, 6],\n",
       "       [6, 7, 5, 4, 9, 7],\n",
       "       [8, 6, 7, 7, 7, 9],\n",
       "       [2, 4, 5, 0, 1, 9]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c70c66d4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 1, 2, 8, 5, 4],\n",
       "       [0, 5, 1, 9, 5, 0],\n",
       "       [7, 5, 2, 1, 8, 1],\n",
       "       [4, 2, 4, 5, 3, 7]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3=np.random.randint(0,10,(4,6))\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5d37209b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 8, 9, 2, 7, 6],\n",
       "       [6, 7, 5, 4, 9, 7],\n",
       "       [8, 6, 7, 7, 7, 9],\n",
       "       [2, 4, 5, 0, 1, 9],\n",
       "       [7, 1, 2, 8, 5, 4],\n",
       "       [0, 5, 1, 9, 5, 0],\n",
       "       [7, 5, 2, 1, 8, 1],\n",
       "       [4, 2, 4, 5, 3, 7]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((arr2,arr3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7f7971d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 8, 9, 2, 7, 6, 7, 1, 2, 8, 5, 4],\n",
       "       [6, 7, 5, 4, 9, 7, 0, 5, 1, 9, 5, 0],\n",
       "       [8, 6, 7, 7, 7, 9, 7, 5, 2, 1, 8, 1],\n",
       "       [2, 4, 5, 0, 1, 9, 4, 2, 4, 5, 3, 7]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((arr2,arr3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8986a393",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 8, 9, 2, 7, 6],\n",
       "       [6, 7, 5, 4, 9, 7],\n",
       "       [8, 6, 7, 7, 7, 9],\n",
       "       [2, 4, 5, 0, 1, 9],\n",
       "       [7, 1, 2, 8, 5, 4],\n",
       "       [0, 5, 1, 9, 5, 0],\n",
       "       [7, 5, 2, 1, 8, 1],\n",
       "       [4, 2, 4, 5, 3, 7]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((arr2,arr3),axis=0) # 这个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "25db33cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[5, 8, 9],\n",
       "        [6, 7, 5],\n",
       "        [8, 6, 7],\n",
       "        [2, 4, 5]]),\n",
       " array([[2, 7, 6],\n",
       "        [4, 9, 7],\n",
       "        [7, 7, 9],\n",
       "        [0, 1, 9]])]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(arr2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d08c5c79",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[5, 8, 9, 2, 7, 6],\n",
       "        [6, 7, 5, 4, 9, 7]]),\n",
       " array([[8, 6, 7, 7, 7, 9],\n",
       "        [2, 4, 5, 0, 1, 9]])]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(arr2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "783ed020",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[5, 8, 9, 2, 7, 6],\n",
       "        [6, 7, 5, 4, 9, 7]]),\n",
       " array([[8, 6, 7, 7, 7, 9],\n",
       "        [2, 4, 5, 0, 1, 9]])]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(arr2,2,axis=0) # 这个"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2af911ff",
   "metadata": {},
   "source": [
    "# 线性计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "423dda54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[5, 8, 9, 2, 7, 6],\n",
       "        [6, 7, 5, 4, 9, 7],\n",
       "        [8, 6, 7, 7, 7, 9],\n",
       "        [2, 4, 5, 0, 1, 9]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat1=np.mat(arr2)\n",
    "mat1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8301d83a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[7, 1, 2, 8, 5, 4],\n",
       "        [0, 5, 1, 9, 5, 0],\n",
       "        [7, 5, 2, 1, 8, 1],\n",
       "        [4, 2, 4, 5, 3, 7]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat2=np.mat(arr3)\n",
    "mat2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c8702442",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[136, 164, 203,  69],\n",
       "        [102, 121, 135,  30],\n",
       "        [157, 170, 172,  61],\n",
       "        [145, 154, 191, 102]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat2*(mat1.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "3cfd97d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 0.11868984, -0.07877569,  0.03490463, -0.06213655],\n",
       "        [-0.12911466,  0.06915055,  0.06091464,  0.0631154 ],\n",
       "        [-0.06741154,  0.0019577 ,  0.0113075 ,  0.09314884],\n",
       "        [ 0.08526534,  0.06071637, -0.07124592, -0.03334414],\n",
       "        [-0.01088065,  0.02116845,  0.06506652, -0.02172573],\n",
       "        [-0.04865267, -0.02830233, -0.02080682,  0.14023113]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mat2.I"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
